<?php
// $Id: MemberController.php 644 2013-01-20 10:21:08Z tomqin@gmail.com $

/**
 * Part of the framework.
 *
 * @package     Controller
 * @version     1.0
 * @copyright   Copyright (C) 2010-2012 SupeFun.com Inc.
 * @author      TomQin <tomqin@gmail.com>
 * @license     http://www.supefun.com/license/
 */

class MemberController extends Controller
{
    public $shopInfo = array(); //店铺信息
    public $userInfo = array(); //用户信息

    //在中间加了一层
    public function tpl($tpl) {
        return parent::tpl('member.'.$tpl);
    }

    public function beforeAction() {
        if($this->module != 'system') {

            $this->userInfo = Base::getObject('service.WinduserModule')->initUser();

            if(!$this->checkLogined()) { //未登录
                header("Location: /?m=u&c=login&backurl=".urlencode('/account.php'));
                exit;
            }

            //取得shopId
            $sql = Db::buildSql('SELECT shop_id FROM `%s` WHERE owner_uid = %d', Db::table('shop'), $this->userInfo['uid']);
            $this->shopId = Db::getFirst($sql);

            if($this->controller != 'create') {
                if(!$this->shopId) { //未绑定
                    $this->renderMessage('尚未申请店铺，<a href="?m=biz&c=create">点击这里申请</a>');
                }

                if(!$this->checkBusiness()) { //未审核通过，等待审核
                    $this->renderMessage('您的店铺申请已提交，正在进行审核');
                }
            }
        }
    }

    public function indexAction() {
        header('Location: /u-login-run');
        exit;
    }

    public function afterAction() {}

    public function renderMessage($message) {
        $this->setVar('message', $message);
        $this->render('m_message');
    }

    //判断是否为商户用户
    public function checkBusiness() {
        return in_array($this->userInfo['groupid'], array(16,17));
    }

    //判断是否为登录用户
    public function checkLogined() {
        if(isset($this->userInfo['uid']) && $this->userInfo['uid'] >0){
            return true;
        }else{
            return false;
        }
    }

    static public function instance() {
        if('i' !== substr($_SERVER['HTTP_HOST'], 0, -13)){
            //header('Location: http://www.sudingke.com'.$_SERVER['REQUEST_URI']);
            exit('WRONG HTTP_HOST');
        }

        $module     = get_request('m', 'default');
        $controller = get_request('c', 'main');
        $action     = get_request('a', 'index');

        $object  = Controller::loadController('member', $module, $controller);
        if(!($object && $object instanceof MemberController)){
            $object = new MemberController;
        }
        $object->setAction($action);
        $object->run();
    }

}
